package com.springboot.springSessionDemo.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hewei<br>
 *         Date 2018年8月27日<br>
 *         <p>
 *         记住用户登录一般有两种方法：
 *         <ul>
 *         <li>设置客户端上保存session的cookie的失效时间</li>
 *         <li>把用户的用户名和密码加密通过cookie保存到客户端，用户登录时使用已保存的cookie中的用户名和密码登录</li>
 *         </ul>
 *         </p>
 */
@RestController
public class TestController {

	private Logger log = LoggerFactory.getLogger(TestController.class);

	@RequestMapping(value = "/first", method = RequestMethod.GET)
	public Map<String, Object> first(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		map.put("request Url", request.getRequestURL());
		HttpSession session = request.getSession();
		session.setAttribute("map", map);
		Cookie[] cookies = request.getCookies();
		if (cookies.length != 0) {
			log.info("cookiesAndValues-------------------------");
			Arrays.asList(cookies).forEach(cookie -> {
				log.info(cookie.getName() + ":" + cookie.getValue());
			});
		}
		log.info("sessionidAndValues--------------------------------");
		log.info(session.getId());
		return map;
	}

	@RequestMapping(value = "/remember", method = RequestMethod.GET)
	public Map<String, Object> remember(HttpServletRequest request, HttpServletResponse respose) {
		Map<String, Object> map = new HashMap<>();
		HttpSession session = request.getSession();
		Cookie[] cookies = request.getCookies();
		log.info("setCookie maxAge=60*60*24 -------------------------");
//		通过cookie设置24小时记住我
		if (cookies.length != 0) {
			Arrays.asList(cookies).forEach(cookie -> {
				if (cookie.getName().equals("SESSION")) {
					cookie.setMaxAge(60 * 60 * 24);
					respose.addCookie(cookie);
				}
			});
		}
		return map;
	}

	@RequestMapping(value = "/forget", method = RequestMethod.GET)
	public Map<String, Object> forget(HttpServletRequest request, HttpServletResponse respose) {
		Map<String, Object> map = new HashMap<>();
		HttpSession session = request.getSession();
		Cookie[] cookies = request.getCookies();
		log.info("setCookie maxAge=0 -------------------------");
//		通过cookie设置登录失效
		if (cookies.length != 0) {
			Arrays.asList(cookies).forEach(cookie -> {
				if (cookie.getName().equals("SESSION")) {
					cookie.setMaxAge(0);
					respose.addCookie(cookie);
				}
			});
		}
		return map;
	}

	@RequestMapping(value = "/sessions", method = RequestMethod.GET)
	public Object sessions(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		HttpSession session = request.getSession();
		map.put("sessionId", session.getId());
		map.put("message", session.getAttribute("map"));
		Cookie[] cookies = request.getCookies();
		if (cookies.length != 0) {
			log.info("cookiesAndValues-------------------------");
			Arrays.asList(cookies).forEach(cookie -> {
				log.info(cookie.getName() + ":" + cookie.getValue());
			});
		}
		log.info("sessionid--------------------------------");
		log.info(session.getId());
		return map;
	}
}
